package com.apache.rpc.service.impl;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.rpc.entity.RpcMonitoring;
import com.apache.rpc.manager.RpcMonitoringManager;
import com.apache.rpc.service.impl.helper.CacheTactics;

import java.util.List;

public class RpcMonitoringManagerImpl implements RpcMonitoringManager {

    protected IDao rpcMonitoringDao;

    protected final String entityName = "com.apache.rpc.entity.RpcMonitoring";

    protected CacheTactics interfaceRegisterCacheTactics;

    public void setRpcMonitoringDao(IDao rpcMonitoringDao) {
        this.rpcMonitoringDao = rpcMonitoringDao;
    }

    public String saveInfo(ParamsVo<RpcMonitoring> vo) throws BusinessException {
        RpcMonitoring monitoring = vo.getObj();
        String infoId = Validator.generate();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        String cacheKey = "";
        //String cacheKey = "interfaceRegister_" + infoId;
        monitoring.setMonitoringId(infoId);
        MethodParam param = new MethodParam("RpcMonitoring", cacheKey, "", entityName);
        param.setVaule(monitoring);
        if (rpcMonitoringDao.insert(param)) {
            return infoId;
        }
        return "";
    }

    public boolean editInfo(ParamsVo<RpcMonitoring> vo) throws BusinessException {
        RpcMonitoring monitoring = vo.getObj();
        if (Validator.isNotNull(monitoring.getMonitoringId())) {
            String cacheKey = "";
            // String cacheKey = "interfaceRegister_" + interfaceRegister.getRegisterId();
            MethodParam param = new MethodParam("RpcMonitoring", cacheKey, "", entityName);
            param.setVaule(monitoring);
            boolean mark = rpcMonitoringDao.edit(param);
            return mark;
        }
        return false;
    }

    public boolean deleteInfo(ParamsVo<RpcMonitoring> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        String cacheKey = "";
        //String cacheKey = "interfaceRegister_" + infoId;
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        RpcMonitoring info = (RpcMonitoring) rpcMonitoringDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        boolean flag = false;
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("RpcMonitoring");
            flag = rpcMonitoringDao.edit(param);
        } else {
            param.setParams("monitoringId", infoId);
            param.setDelete(true);
            flag = rpcMonitoringDao.delete(param);
        }
        return flag;
    }

    public Object getInfoById(ParamsVo<RpcMonitoring> vo) {
        String infoId = vo.getInfoId();
        String cacheKey = "";
        //String cacheKey = "interfaceRegister_" + infoId;
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return rpcMonitoringDao.selectById(param);
    }

    public Object execute(ParamsVo<RpcMonitoring> vo) {
        return null;
    }

    public Page getPageInfo(ParamsVo<RpcMonitoring> vo) {
        MethodParam param = setMethodParams(vo, 2);
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = rpcMonitoringDao.pageSelect(param);
        return page;
    }

    @SuppressWarnings("unchecked")
    public List<RpcMonitoring> getList(ParamsVo<RpcMonitoring> vo) {
        MethodParam param = setMethodParams(vo, 2);
        return rpcMonitoringDao.select(param);
    }

    public long countInfo(ParamsVo<RpcMonitoring> vo) {
        MethodParam param = setMethodParams(vo, 1);
        return rpcMonitoringDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<RpcMonitoring> vo, int type) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuffer sb = new StringBuffer(rpcMonitoringDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        RpcMonitoring monitoring = vo.getObj();
        if (Validator.isEmpty(monitoring))
            return param;
        if (Validator.isNotNull(monitoring.getBeanId())) {
            sb.append(" and beanId = :beanId");
            param.setParams("beanId", monitoring.getBeanId());
        }
        if (Validator.isNotNull(monitoring.getMethodKey())) {
            sb.append(" and methodKey = :methodKey");
            param.setParams("methodKey", monitoring.getMethodKey());
        }
        if (Validator.isNotNull(monitoring.getServerAddr())) {
            sb.append(" and serverAddr =:serverAddr");
            param.setParams("serverAddr", monitoring.getServerAddr());
        }
        if (Validator.isNotNull(monitoring.getMonitoring1())) {
            sb.append(" and monitoring1 = :monitoring1");
            param.setParams("monitoring1", monitoring.getMonitoring1());
        }
        if (Validator.isNotNull(monitoring.getMonitoring2())) {
            sb.append(" and monitoring2 = :monitoring2");
            param.setParams("monitoring2", monitoring.getMonitoring2());
        }

        param.setSqlStr(sb.toString());
        return param;
    }

}
